<template>
  <div class="artist-list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.art_id"
          :title="item.title"
          @click="todetail(item.art_id)"
        >
          <!-- 使用 title 插槽来自定义标题 -->
          <template #label>
            <!-- 主体内容区域 -->
            <van-grid :border="false" :column-num="item.cover.type">
              <!-- 列表项 -->
              <van-grid-item
                v-for="(it, index) in item.cover.images"
                :key="index"
              >
                <van-image fit="cover" :src="it" lazy-load />
              </van-grid-item>
            </van-grid>
            <!-- 底部额外信息区域 -->
            <div class="meta">
              <!-- 左侧 -->
              <div class="info-box">
                <span>{{ item.aut_name }}</span>
                <span>{{ item.comm_count }}评论</span>
                <span>{{ item.pubdate | fromNow }}</span>
              </div>
              <!-- 右侧 -->
              <van-icon name="cross"></van-icon>
            </div>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticle } from '../../api/index'
// import moment from 'moment'
// 设置语言环境 i18n
// moment.locale('zh-cn') // 中文
export default {
  name: 'articleList',
  props: ['channelId'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      timestamp: undefined
    }
  },
  /* filters: {
    fromNow (values) {
      return moment(values).fromNow()
    }
  }, */
  methods: {
    // 跳转文章详情
    todetail (id) {
      this.$router.push({ name: 'articles', params: { id } })
    },
    async onLoad () {
      if (this.timestamp === undefined) {
        this.timestamp = Date.now()
      }
      const res = await getArticle({
        channel_id: this.channelId,
        timestamp: this.timestamp
      })
      // console.log(res)
      if (this.refreshing) {
        this.list = []
        this.refreshing = false
      }
      this.list.push(...res.data.data.results)
      this.timestamp = res.data.data.pre_timestamp
      this.loading = false
      if (this.list.length >= 100) {
        this.finished = true
      }
      /* setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1)
        }
        this.loading = false

        if (this.list.length >= 40) {
          this.finished = true
        }
      }, 1000) */
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    }
  }
}
</script>

<style lang="less">
.artist-list {
  .meta {
    display: flex;
    justify-content: space-between;
    span {
      margin-right: 10px;
    }
  }
}
</style>
